package com.example.memo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private LinearLayout ll_home,ll_search,ll_mine;
    private ImageView iv_home,iv_search,iv_mine;
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initEvent();
    }

    public void initView(){
        ll_home = findViewById(R.id.ll_home);
        ll_search = findViewById(R.id.ll_search);
        ll_mine = findViewById(R.id.ll_mine);
        iv_home = findViewById(R.id.iv_home);
        iv_search = findViewById(R.id.iv_search);
        iv_mine = findViewById(R.id.iv_mine);

    }

    public void initEvent(){
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        HomeFragment homeFragment = new HomeFragment();
        fragmentTransaction.replace(R.id.frag_container,homeFragment).commit();
        iv_home.setSelected(true);

        ll_home.setOnClickListener(this);
        ll_search.setOnClickListener(this);
        ll_mine.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if(id == R.id.ll_home){
            fragmentManager = getSupportFragmentManager();
            fragmentTransaction = fragmentManager.beginTransaction();
            HomeFragment homeFragment = new HomeFragment();
            fragmentTransaction.replace(R.id.frag_container,homeFragment).commit();
            setSelect(iv_home);
        }else if(id == R.id.ll_search){
            fragmentManager = getSupportFragmentManager();
            fragmentTransaction = fragmentManager.beginTransaction();
            SearchFragment searchFragment = new SearchFragment();
            fragmentTransaction.replace(R.id.frag_container,searchFragment).commit();
            setSelect(iv_search);
        }else if(id == R.id.ll_mine){
            fragmentManager = getSupportFragmentManager();
            fragmentTransaction = fragmentManager.beginTransaction();
            MineFragment mineFragment = new MineFragment();
            fragmentTransaction.replace(R.id.frag_container,mineFragment).commit();
            setSelect(iv_mine);
        }
    }

    private void setSelect(ImageView iv){
        iv_home.setSelected(false);
        iv_search.setSelected(false);
        iv_mine.setSelected(false);
        iv.setSelected(true);
    }
}